package com.ethannhu.todaynews.component.discovery

import android.content.Intent
import android.os.Bundle
import com.ethannhu.todaynews.adapter.TabLayoutViewPager2Mediator
import com.ethannhu.todaynews.component.content.ContentFragment
import com.ethannhu.todaynews.component.main.MainActivity
import com.ethannhu.todaynews.component.publish.PublishActivity
import com.ethannhu.todaynews.databinding.FragmentDiscoveryBinding
import com.ethannhu.todaynews.fragment.BaseViewModelFragment
import com.ethannhu.todaynews.util.DataUtil

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * 主页界面
 *  * 流程：
 *  1. 设置 Viewpager 的 Adapter
 *  2. Adapter自FragmentStateAdapter
 *  3. createFragment返回每一个 Fragment的实例
 *  4. 关联 TabLayout 和 Viewpager 之间的联系
 */
class DiscoveryFragment : BaseViewModelFragment<FragmentDiscoveryBinding>() {
    override fun initData() {
        super.initData()
        binding.apply {
            pager.adapter = DiscoveryAdapter(requireActivity(), DataUtil.categories)
            TabLayoutViewPager2Mediator(indicator, pager) { indicator, pager ->
                //没啥配置
            }.attach()
        }


    }

    override fun initListeners() {
        super.initListeners()
        binding.menu.setOnClickListener {
            (hostActivity as MainActivity).openDrawer()
        }

        binding.add.setOnClickListener {
            loginAfter {
                startActivity(Intent(requireActivity(), PublishActivity::class.java))
            }
        }
    }

    companion object {
        @JvmStatic //可加可不加，主要看要不要和 Java 代码交互
        fun newInstance() =
            DiscoveryFragment().apply {
                arguments = Bundle().apply {
                }
            }
    }
}